home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / wu_ftpd_37_21.lha / wu-ftpd / src / realpath.c < prev    next >
C/C++ Source or Header  |  1994-07-29  |  5KB  |  193 lines

  1. /* Copyright (c) 1993, 1994  Washington University in Saint Louis
  2.  * All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are
  6.  * met: 1. Redistributions of source code must retain the above copyright
  7.  * notice, this list of conditions and the following disclaimer. 2.
  8.  * Redistributions in binary form must reproduce the above copyright notice,
  9.  * this list of conditions and the following disclaimer in the documentation
  10.  * and/or other materials provided with the distribution. 3. All advertising
  11.  * materials mentioning features or use of this software must display the
  12.  * following acknowledgement: This product includes software developed by the
  13.  * Washington University in Saint Louis and its contributors. 4. Neither the
  14.  * name of the University nor the names of its contributors may be used to
  15.  * endorse or promote products derived from this software without specific
  16.  * prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY WASHINGTON UNIVERSITY AND CONTRIBUTORS
  19.  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21.  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASHINGTON
  22.  * UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24.  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  28.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. #ifdef AMIGA
  33. #include <proto/dos.h>
  34. #include <string.h>
  35. #include "config.h"
  36.  
  37. char *
  38. realpath(char *pathname, char *result)
  39. {
  40.   BPTR lock;
  41.  
  42.   if(lock = Lock(pathname, SHARED_LOCK))
  43.   {
  44.     if (!(NameFromLock(lock, result, MAXPATHLEN)))
  45.     {
  46.       *result = 0;
  47.       result = NULL;
  48.     }
  49.     UnLock(lock);
  50.   }
  51.   else
  52.   {
  53.     *result = 0;
  54.     result = NULL;
  55.    }
  56.   return result;
  57. }
  58.  
  59. #else
  60.  
  61. #include "config.h"
  62.  
  63. #include <stdio.h>
  64. #include <sys/types.h>
  65. #include <sys/stat.h>
  66. #include <sys/param.h>
  67. #include <string.h>
  68.  
  69. #ifndef HAVE_SYMLINK
  70. #define lstat stat
  71. #endif
  72.  
  73. char *
  74. realpath(char *pathname, char *result)
  75. {
  76.     struct stat sbuf;
  77.     char curpath[MAXPATHLEN],
  78.       workpath[MAXPATHLEN],
  79.       linkpath[MAXPATHLEN],
  80.       namebuf[MAXPATHLEN],
  81.      *where,
  82.      *ptr,
  83.      *last;
  84.     int len;
  85.  
  86.     strcpy(curpath, pathname);
  87.  
  88.     if (*pathname != '/') {
  89.         uid_t userid;
  90.         
  91. #ifdef HAVE_GETCWD
  92.     if (!getcwd(workpath,MAXPATHLEN)) {
  93. #else
  94.         if (!getwd(workpath)) {
  95. #endif    
  96.         userid = geteuid();
  97.         seteuid(0);
  98. #ifdef HAVE_GETCWD
  99.             if (!getcwd(workpath,MAXPATHLEN)) {
  100. #else
  101.             if (!getwd(workpath)) {
  102. #endif
  103.                 strcpy(result, ".");
  104.             seteuid(userid);
  105.                 return (NULL);
  106.             }
  107.         seteuid(userid);
  108.     }
  109.     } else
  110.         *workpath = NULL;
  111.  
  112.     /* curpath is the path we're still resolving      */
  113.     /* linkpath is the path a symbolic link points to */
  114.     /* workpath is the path we've resolved            */
  115.  
  116.   loop:
  117.     where = curpath;
  118.     while (*where != NULL) {
  119.         if (!strcmp(where, ".")) {
  120.             where++;
  121.             continue;
  122.         }
  123.         /* deal with "./" */
  124.         if (!strncmp(where, "./", 2)) {
  125.             where += 2;
  126.             continue;
  127.         }
  128.         /* deal with "../" */
  129.         if (!strncmp(where, "../", 3)) {
  130.             where += 3;
  131.             ptr = last = workpath;
  132.             while (*ptr) {
  133.                 if (*ptr == '/')
  134.                     last = ptr;
  135.                 ptr++;
  136.             }
  137.             *last = NULL;
  138.             continue;
  139.         }
  140.         ptr = strchr(where, '/');
  141.         if (!ptr)
  142.             ptr = where + strlen(where) - 1;
  143.         else
  144.             *ptr = NULL;
  145.  
  146.         strcpy(namebuf, workpath);
  147.         for (last = namebuf; *last; last++)
  148.             continue;
  149.         if (*--last != '/')
  150.             strcat(namebuf, "/");
  151.         strcat(namebuf, where);
  152.  
  153.         where = ++ptr;
  154.         if (lstat(namebuf, &sbuf) == -1) {
  155.             strcpy(result, namebuf);
  156.             return (NULL);
  157.         }
  158.         /* was IFLNK */
  159. #ifdef HAVE_SYMLINK
  160.         if ((sbuf.st_mode & S_IFMT) == S_IFLNK) {
  161.             len = readlink(namebuf, linkpath, MAXPATHLEN);
  162.             if (len == 0) {
  163.                 strcpy(result, namebuf);
  164.                 return (NULL);
  165.             }
  166.             *(linkpath + len) = NULL;   /* readlink doesn't null-terminate
  167.                                          * result */
  168.             if (*linkpath == '/')
  169.                 *workpath = NULL;
  170.             if (*where) {
  171.                 strcat(linkpath, "/");
  172.                 strcat(linkpath, where);
  173.             }
  174.             strcpy(curpath, linkpath);
  175.             goto loop;
  176.         }
  177. #endif
  178.         if ((sbuf.st_mode & S_IFDIR) == S_IFDIR) {
  179.             strcpy(workpath, namebuf);
  180.             continue;
  181.         }
  182.         if (*where) {
  183.             strcpy(result, namebuf);
  184.             return (NULL);      /* path/notadir/morepath */
  185.         } else
  186.             strcpy(workpath, namebuf);
  187.     }
  188.     strcpy(result, workpath);
  189.     return (result);
  190. }
  191.  
  192. #endif
  193.